/**
 * xml的通用函数.
 * 提供动态创建，解析xml字符串获取节点值和属性值功能
 * @example
var xmlstr = "<response sessionID='48C14D02578E9D28D3E897671378E331' status='ok'><aa id='a1' cc='dd'></aa><aa id='a2' cc='ee'></aa><bb id='b1'></bb><title>111</title><title>222</title><title></title><title>444</title></response>";
var xml = coos.xml.parse(xmlstr);

var root = coos.xml.root(xml);
//根节点的属性不能使用coos.xml.attribute.values获得
//alert(coos.xml.attribute.value(root,"sessionID"));//48C14D02578E9D28D3E897671378E331

var aa = coos.xml.node(xml,"aa");
//alert(coos.xml.attribute.value(aa,"cc"));//dd
//alert(coos.xml.nodes(xml,"aa").length);//2

var nodes = coos.xml.childNodes(xml);
//alert(nodes.length);//7

//alert(coos.xml.node.value(xml,"title"));//111

//alert(coos.xml.node.values(xml,"title"));//111,222,,444
//alert(coos.xml.attribute.values(xml,"aa","id"));//a1,a2
//alert(coos.xml.attribute.values(root,"aa","id"));//a1,a2
//alert(coos.xml.attribute.values(nodes,"aa","id"));//a1,a2

//动态创建并解析xml
var xml2 = coos.xml.create('<xml id="xx"><book><title>creat xml test</title></book></xml>');
var book = coos.xml.node(xml2,"book");
alert(coos.xml.node.value(book,"title"));//creat xml test
 * 
 */
coos.xml = function(){};

/**
 * 动态创建xml
 * @example
 *  var xml = coos.xml.create('<xml id="xx"><book><title>creat xml test</title></book></xml>');
 */
coos.xml.create = function (str)
{
	if(typeof DOMParser !== "undefined")
	{
		return (new DOMParser()).parseFromString(str, "application/xml");
	}
	else if(typeof ActiveXObject != "undefined")
	{
		//在create上设置一个静态成员
		if(typeof arguments.callee.activeXString !== "string")
	    {
	    	var versions = ["MSXML2.DOMDocument.6.0",
	         "MSXML2.DOMDocument.5.0",
	         "MSXML2.DOMDocument.4.0",
	         "MSXML2.DOMDocument.3.0",
	         "MSXML2.DOMDocument",
	         "Microsoft.XMLDOM"];
			for(var i=0,len=versions.length; i < len; i++)
			{
				try
				{
					var xml = new ActiveXObject(versions[i]);
					arguments.callee.activeXString = versions[i];
					xml = null;
				}
				catch (e){}
			}
	     }
	     xml = new ActiveXObject(arguments.callee.activeXString);
	     xml.async = "false";
	     xml.loadXML(str);
	     return xml;
   }
};

/**
 * 解析xml,把xml字符串转换成对象
 */
coos.xml.parse = function (xmlContent){
	var xml = null;
	try
	{ 
		xml = new DOMParser();
		return xml.parseFromString(xmlContent,"text/xml");;
	}
	catch(e)
	{
		xml = new ActiveXObject("Microsoft.XMLDOM");
		xml.async = false;
		xml.loadXML(xmlContent);
		return xml;
	}
};

/**
 * 获取xml根节点
 * @param xml xml对象
 */
coos.xml.root = function (xml)
{
	if (xml == null){return null;}	
	return xml.documentElement;
};

/**
 * 格式化节点对象，传入xml或node对象都返回node对象
 */
coos.xml.format = function(obj)
{
	if(obj == null || typeof(obj) == "undefined"){return null;}
	//如果存在documentElement属性说明是xml对象,处理成节点对象后返回
	if(typeof(obj.documentElement) == "object"){
		obj = obj.documentElement;
	}
	return obj;
};

/**
 * 获得对应名称的第所有节点
 */
coos.xml.nodes = function(obj,nodeName)
{
	var node = coos.xml.format(obj);
	if(node == null){return null;}
	
	return node.getElementsByTagName(nodeName);
};

/**
 * 获得对应名称的第一个节点
 */
coos.xml.node = function(obj,nodeName)
{
	var node = coos.xml.format(obj);
	if(node == null){return null;}
	
	return node.getElementsByTagName(nodeName)[0];
};

/**
 * 返回对应名称节点的第一个值
 * @param obj xml对象或node对象
 * @param nodeName 节点名称
 */
coos.xml.node.value = function(obj,nodeName)
{
	var node = coos.xml.format(obj);
	if(node == null){return "";}
	
	var element = node.getElementsByTagName(nodeName);
	var value = "";
	if(element[0].firstChild != null)
	{
		value = element[0].firstChild.nodeValue;
	}
	return value;
};

/**
 * 返回对应名称节点的所有值
 * @param obj xml对象或node对象
 * @param nodeName 节点名称
 */
coos.xml.node.values = function(obj,nodeName)
{
	var node = coos.xml.format(obj);
	if(node == null){return null;}
	
	var nodes = node.getElementsByTagName(nodeName);
	var len = nodes.length;
	var values = [];
	for(var i = 0; i < len; i++){
		if(nodes[i].firstChild != null)
		{
			values.push(nodes[i].firstChild.nodeValue);
		}else{
			values.push("");
		}
	}
	return values;
};

/**
 * 获取子节点
 * @param node 节点或xml对象
 */
coos.xml.childNodes = function(obj)
{
	var node = coos.xml.format(obj);
	if(node == null){return null;}
	
	var temp = [];
	var nodes = node.childNodes;
	
	for(var i = 0; i < nodes.length; i++)
	{
		if(nodes[i].nodeType == 1)
		{
			temp.push(nodes[i]);
		}
	}
	return temp;
};

/**
 * 节点属性相关函数
 */
coos.xml.attribute = function(){};

/**
 * 返回节点对应属性的第一个值
 * @param obj xml对象或node对象
 * @param attributeName 属性名称
 */
coos.xml.attribute.value = function(obj,attributeName)
{
	var node = coos.xml.format(obj);
	if(node == null){return "";}
	
	return node.getAttribute(attributeName);
};

/**
 * 返回节点对应属性的所有值
 * @param nodes xml对象、node节点或node对象数组
 * @param nodeName节点名称
 * @param attributeName 属性名称
 */
coos.xml.attribute.values = function(nodes,nodeName,attributeName)
{
	//如果长度属性不存在证明不是节点对象数组,则自动获取
	if(typeof(nodes.length) == "undefined"){
		nodes = coos.xml.childNodes(nodes);
	}
	var len = nodes.length;
	var values = [];
	for(var i = 0; i < len; i++)
	{
		if(nodes[i].nodeName == nodeName && nodes[i].getAttribute(attributeName) != null)
		{
			values.push(nodes[i].getAttribute(attributeName));
		}
	}
	return values;
};

/**
 * 清除对象，释放内存
 */
coos.xml.clear = function(obj)
{
	obj = null;
};





